Imports System.Windows.Forms

Public Class frmTracking

    Public Enum enumTrackingMode
        Create
        Edit
    End Enum

    Private da As DbDataAdapter

    Private Mode As enumTrackingMode
    Private ThisRow As DataRow
    Private WorkingTable, ImportTable As String

    ''' <summary>
    ''' Instantiate form
    ''' </summary>
    ''' <param name="TrackingMode">Controls the form's display mode</param>
    ''' <param name="WorkingName">Name of existing Working Table (blank for new)</param>
    ''' <param name="ImportName">If mode is Import, pass name of file that was imported</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal TrackingMode As enumTrackingMode, ByVal WorkingName As String, Optional ByVal ImportName As String = "")
        InitializeComponent()
        WorkingTable = WorkingName
        ImportTable = ImportName
        Mode = TrackingMode
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Close()
    End Sub

    Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click
        ShowHelp(Me, "13")
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        With Project
            If Project.DB.DatabaseType = clsDB.enumDBType.Oracle And cboTableGroup.Text.Trim = "" Then
                WarningMsg("You must specify a table group name for Oracle databases.")
                Exit Sub
            End If
            WorkingTable = txtTableName.Text
            ThisRow.Item("Temp_DB_Name") = WorkingTable
            ThisRow.Item("Description") = txtDescription.Text
            ThisRow.Item("Source") = txtSource.Text
            ThisRow.Item("QCode") = .GetPickList(cboQCode)
            ThisRow.Item("Q_Info") = txtAssessment.Text
            ThisRow.Item("Q_Info") = txtAssessment.Text
            ThisRow.Item("TableGroup") = cboTableGroup.Text
            ThisRow.Item("Create_Update") = Now
            Try
                da.Update(Project.DataSet.Tables("Tracking"))
                .DataSet.Tables("Tracking").AcceptChanges()
            Catch ex As Exception
                'ErrorMsg("An error occurred when trying to update the Support table: Tracking", ex)
            End Try
            DialogResult = Windows.Forms.DialogResult.OK
            Close()
        End With
    End Sub

    Private Sub frmTracking_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveWindowPos(REGAPP, Me)
    End Sub

    Private Sub frmTracking_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            GetWindowPos(REGAPP, Me)
            txtTableName.Text = WorkingTable
            txtTableName.MaxLength = Project.dsWRDB.Tables("TRACKING").Columns("TEMP_DB_NAME").MaxLength
            Select Case Mode
                Case enumTrackingMode.Create : Text = "New Working Table"
                Case enumTrackingMode.Edit : Text = "Edit Working Table"
            End Select

            da = Project.DB.AddTable(Project.DataSet, "Tracking", "Select * from Tracking")
            If da Is Nothing Then Exit Sub
            ThisRow = Nothing
            With Project.DataSet.Tables("Tracking")
                'try to find an existing record matching working and master table
                .CaseSensitive = False
                Dim rTbl() As DataRow = .Select(String.Format("Temp_DB_Name='{0}' AND Master_DB_Name='{1}'", WorkingTable, Project.MasterTableName))
                If rTbl.Length = 1 Then
                    ThisRow = rTbl(0)
                Else 'if can't find, just select first matching working table entry
                    rTbl = .Select(String.Format("Temp_DB_Name='{0}'", WorkingTable))
                    If rTbl.Length > 0 Then ThisRow = rTbl(0)
                End If
                If ThisRow Is Nothing Then
                    ThisRow = .Rows.Add
                    ThisRow.Item("Temp_Db_Name") = WorkingTable
                    ThisRow.Item("Master_DB_Name") = Project.MasterTableName
                End If
            End With
            txtTableName.Text = ThisRow.Item("Temp_Db_Name").ToString
            txtDescription.Text = ThisRow.Item("Description").ToString
            txtSource.Text = ThisRow.Item("Source").ToString
            Project.FillPickList(cboQCode, "", "QCodes", "QCode", "QCode_Name", , ThisRow.Item("QCode").ToString)
            txtAssessment.Text = ThisRow.Item("Q_Info").ToString
            Project.FillPickList(cboTableGroup, "", "Tracking", "TableGroup", "")
            cboTableGroup.Text = ThisRow.Item("TableGroup").ToString

            txtTableName.Enabled = Mode = enumTrackingMode.Create

        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub txtTableName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTableName.Validating
        If Mode <> enumTrackingMode.Create Then Exit Sub
        Dim TableName As String = Project.DB.GetValidTableName(txtTableName.Text)
        If TableName = "" Then e.Cancel = True : Exit Sub
        Dim arReserved() As String = {clsProject.TempData, clsProject.TempView, clsProject.RptData, clsProject.ExtData}
        For Each s As String In arReserved
            If String.Equals(s, TableName, StringComparison.InvariantCultureIgnoreCase) Then
                WarningMsgFmt("The table name you entered is reserved: {0}; please respecify.", s)
                e.Cancel = True
                Exit Sub
            End If
        Next
        If Project.DB.TableExists(TableName) Then
            If WarningMsg("This table already exists; do you want to overwrite it?") = Windows.Forms.DialogResult.OK Then
                Project.DB.DropTable(TableName)
            Else
                e.Cancel = True
                Exit Sub
            End If
        End If
        txtTableName.Text = TableName
    End Sub

    Private Sub btnJournal_Click(sender As System.Object, e As System.EventArgs) Handles btnJournal.Click
        frmJournal.GetJournalDescription()
    End Sub

    ''' <summary>
    ''' Make first character upper case
    ''' </summary>
    Private Sub txt_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTableName.TextChanged, txtAssessment.TextChanged, txtDescription.TextChanged, txtSource.TextChanged, cboTableGroup.TextChanged
        Dim txt As Object = sender
        If txt.Text.Length = 1 Then
            txt.Text = txt.Text.ToUpper
            txt.SelectionStart = 1
        End If
    End Sub
End Class
